﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;


public partial class Main_Company_NewCompany : System.Web.UI.Page
{
    Datautility dut = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (Session["userid"] == null)
        {
            //ScriptManager.RegisterStartupScript(Page, this.GetType(), "redirect", "window.open('../../login.aspx','_parent');", true);
            Response.Redirect("~/SessionExpire.aspx");
        }
        if (CHKVat.Checked == false)
        {
            TxtBoxVat.Enabled = false;
            TxtVatDate.Enabled = false;
        }
        else
        {
            TxtBoxVat.Enabled = true;
            TxtVatDate.Enabled = true;
        }
        if (CHKInterstate.Checked == false)
        {
            TextBoxinnerState.Enabled = false;
            TextBoxInterStateDate.Enabled = false;
        }
        else
        {
            TextBoxinnerState.Enabled = true;
            TextBoxInterStateDate.Enabled = true;
        }

        if (CHKServiceTax.Checked == false)
        {
            TextBoxServiceTax.Enabled = false;
            TxtDateServiceTax.Enabled = false;
        }
        else
        {
            TextBoxServiceTax.Enabled = true;
            TxtDateServiceTax.Enabled = true;
        }

        if (CHKCentralExcise.Checked == false)
        {
            TextBoxCentralExcise.Enabled = false;
            TxtDateCentralExcise.Enabled = false;
        }
        else
        {
            TextBoxCentralExcise.Enabled = true;
            TxtDateCentralExcise.Enabled = true;
        }

        if (CHKLuxuryTax.Checked == false)
        {
            TextBoxLuxuryTax.Enabled = false;
            DateLuxuryTax.Enabled = false;
        }
        else
        {
            TextBoxLuxuryTax.Enabled = true;
            DateLuxuryTax.Enabled = true;
        }

        if (!IsPostBack)
        {
            bindBType();
            bindStatus();
            bindstate();
            bindcountry();
            TabContainer1.ActiveTabIndex = 0;
        }
        //if (Session["NewCompId"] == null)
        //{
        //    TabContainer2.Enabled = false;
        //    TabContainer3.Enabled = false;
        //}

    }

    private void bindBType()
    {
        DataTable DT = dut.getdata("select * from ACC_BType");
        DDLTypeOfBuisness.DataSource = DT;
        DDLTypeOfBuisness.DataTextField = "BTypeName";
        DDLTypeOfBuisness.DataValueField = "BTypeId";
        DDLTypeOfBuisness.DataBind();
        ListItem lst = new ListItem("Others", "0");
        DDLTypeOfBuisness.Items.Add(lst);
    }

    private void bindStatus()
    {
        DataTable DT = dut.getdata("select * from ACC_CompStatus");
        DDLStatus.DataSource = DT;
        DDLStatus.DataTextField = "CompStatusName";
        DDLStatus.DataValueField = "CompStatusId";
        DDLStatus.DataBind();
        ListItem ls = new ListItem("Others", "0");
        DDLStatus.Items.Add(ls);
    }

    private void bindcountry()
    {
        DataTable dt = dut.getdata("select * from MAS_Country");
        DDLCountry.DataSource = dt;
        DDLCountry.DataTextField = "CountryName";
        DDLCountry.DataValueField = "CountryId";
        DDLCountry.DataBind();

    }
    private void bindstate()
    {
        DataTable DT = dut.getdata("select * from MAS_State");
        DDLState.DataSource = DT;
        DDLState.DataTextField = "StateName";
        DDLState.DataValueField = "StateId";
        DDLState.DataBind();

    }
    protected void DDLTypeOfBuisness_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDLTypeOfBuisness.SelectedValue == "0")
        {
            TxtOthers.Visible = true;
        }
        else
        {
            TxtOthers.Visible = false;
        }
    }
    protected void DDLStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DDLStatus.SelectedValue == "0")
        {
            TxtStatus.Visible = true;
        }
        else
        {
            TxtStatus.Visible = false;
        }
    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        int account = 0, invoice = 0, inventory = 0;
        int selected = Convert.ToInt16(chk.SelectedValue);
        if (selected == 1)
        {
            account = 1;
        }

        if (selected == 2)
        {
            invoice = 1;
        }

        if (selected == 3)
        {
            inventory = 1;
        }

        string[] arrstartdate = TxtAccountingYearStarting.Text.Split(new char[] { '/' });
        string startdate = arrstartdate[1].ToString() + "/" + arrstartdate[0].ToString() + "/" + arrstartdate[2].ToString();

        string[] arrenddate = TxtAccountingLast.Text.Split(new char[] { '/' });
        string enddate = arrenddate[1].ToString() + "/" + arrenddate[0].ToString() + "/" + arrenddate[2].ToString();
        SqlConnection con = new SqlConnection("Data Source =dis-server; initial catalog = Master; user id = sa; Password = goal;");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "create database [DISACC_" + TextCompanyName.Text+"]";
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();
        DataSet ds =new DataSet();
        ds.ReadXml(Server.MapPath("~/XML/ConnStr.xml"));
        if (ds.Tables.Count > 0)
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr["servername"] = "dis-server";
            dr["companyname"] = TextCompanyName.Text;
            dr["dbname"] = "DISACC_" + TextCompanyName.Text;
            dr["userid"] = "sa";
            dr["password"] = "goal";
            ds.Tables[0].Rows.Add(dr);
            ds.WriteXml(Server.MapPath("~/XML/ConnStr.xml"));
        }
        else
        {
            DataTable dt = new DataTable("connectionstring");
            dt.Columns.Add("servername", typeof(string));
            dt.Columns.Add("companyname", typeof(string));
            dt.Columns.Add("dbname", typeof(string));
            dt.Columns.Add("userid", typeof(string));
            dt.Columns.Add("password", typeof(string));
            DataRow dr = dt.NewRow();
            dr["servername"] = "dis-server";
            dr["companyname"] = TextCompanyName.Text;
            dr["dbname"] = "DISACC_" + TextCompanyName.Text;
            dr["userid"] = "sa"; 
            dr["password"] = "goal";
            dt.Rows.Add(dr);
            ds.Tables.Add(dt);
            ds.WriteXml(Server.MapPath("~/XML/ConnStr.xml"));
         }
        String path = "";
        if (Fld.HasFile)
        {
            string randomnumber = RandomNameGenerator.getRandomName();
            FileInfo file = new FileInfo(Fld.PostedFile.FileName);
            string ext = file.Extension;
            path = "~/Logo" + randomnumber + ext;
            Fld.SaveAs(Server.MapPath(path));

            SqlParameter[] parm ={
                                  new SqlParameter("@CompName", TextCompanyName.Text.Trim()),
                                  new SqlParameter("@TradingName", TxtTradingName.Text.Trim()),
                                  new SqlParameter("@LogoUrl", path.ToString()),
                                  new SqlParameter("@BType", DDLTypeOfBuisness.SelectedValue),
                                  new SqlParameter("@OthBtype",TxtOthers.Text.Trim()),
                                  new SqlParameter("@StatusID",DDLStatus.SelectedValue),
                                  new SqlParameter("@OthStatus",TxtStatus.Text.Trim()),
                                  new SqlParameter("@IsAccEnabled",account),
                                  new SqlParameter("@IsInvcEnabled",invoice),
                                  new SqlParameter("@IsInvrEnabled",inventory),
                                  new SqlParameter("@BooksFromDate",startdate),
                                  new SqlParameter("@BooksEndDate",enddate),
                                  new SqlParameter("@CName",Session["username"].ToString())

                               };

            //Response.Write(startdate);
            //Response.Write(enddate);
            try
            {
                //dut.executeprocedure("usp_insert_ACC_Company", parm);
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Company created successfully...";

                //DataTable DTCompany = dut.getdata("select ident_current('ACC_Company')");
                //Session["NewCompId"] = DTCompany.Rows[0][0].ToString();
                BtnSave.Enabled = false;
                TabContainer1.ActiveTabIndex = 1;
            }

            catch (Exception ex)
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = ex.Message;
            }
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["NewCompId"] != null)
        {


            SqlParameter[] parm = {
                                  new SqlParameter("@CompId",Session["NewCompId"].ToString()),
                                   new SqlParameter("@Add1", TxtAdress1.Text.Trim()),
                                   new SqlParameter("@Add2", TxtAddress2.Text.Trim()),
                                   new SqlParameter("@Add3", TxtAddress3.Text.Trim()),
                                   new SqlParameter("@Add4", TxtAddress4.Text.Trim()),
                                   new SqlParameter("@Pincode", TxtPincode.Text.Trim()),
                                   new SqlParameter("@Country", DDLCountry.SelectedValue),
                                   new SqlParameter("@place", TxtPlace.Text.Trim()),
                                   new SqlParameter("@StateID", DDLState.SelectedValue),
                                   new SqlParameter("@Phone", TextBoxPhone.Text.Trim()),
                                   new SqlParameter("@Fax", TextBoxFax.Text.Trim()),
                                   new SqlParameter("@Email",TextBoxEmail.Text.Trim()),
                                   new SqlParameter("@WebSite", TextBoxWebsite.Text.Trim()),
                                   new SqlParameter("@Activity", TextBoxBuisnessActivity.Text.Trim()),
                                   new SqlParameter("@Misc1", TxtMiscellaneous1.Text.Trim()),
                                   new SqlParameter("@Misc2", TxtMiscellaneous2.Text.Trim())
                                   
                               };
            try
            {
                dut.executeprocedure("usp_insert_ACC_Company1", parm);
                TabContainer1.ActiveTabIndex = 2;
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Company Contact Details Created successfully...";
            }
            catch (Exception ex)
            {
                 lblMsg.Attributes.Remove("class");
                 lblMsg.Attributes.Add("class", "false");
                 lblMsg.Text = ex.Message;
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (Session["NewCompId"] != null)
        {

            string Pan = "", taxdeduct = "", vat = "", StateTax = "", TaxService = "", centralexcise = "", luxury = "";
            if (TxtDatePermanant.Text.Trim() != "")
            {

                string[] PanDate = TxtDatePermanant.Text.Split(new char[] { '/' });
                Pan = PanDate[1].ToString() + "/" + PanDate[0].ToString() + "/" + PanDate[2].ToString();
            }

            if (TxtdateTaxDeduction.Text.Trim() != "")
            {

                string[] dateDedcutionTax = TxtdateTaxDeduction.Text.Split(new char[] { '/' });
                taxdeduct = dateDedcutionTax[1].ToString() + "/" + dateDedcutionTax[0].ToString() + "/" + dateDedcutionTax[2].ToString();
            }

            if (TxtVatDate.Text.Trim() != "")
            {
                string[] dateVat = TxtVatDate.Text.Split(new char[] { '/' });
                vat = dateVat[1].ToString() + "/" + dateVat[0].ToString() + "/" + dateVat[2].ToString();
            }

            if (TextBoxInterStateDate.Text.Trim() != "")
            {
                string[] dateInterState = TextBoxInterStateDate.Text.Split(new char[] { '/' });
                StateTax = dateInterState[1].ToString() + "/" + dateInterState[0].ToString() + "/" + dateInterState[2].ToString();
            }

            if (TxtDateServiceTax.Text.Trim() != "")
            {
                string[] dateServiceTax = TxtDateServiceTax.Text.Split(new char[] { '/' });
                TaxService = dateServiceTax[1].ToString() + "/" + dateServiceTax[0].ToString() + "/" + dateServiceTax[2].ToString();
            }


            if (TxtDateCentralExcise.Text.Trim() != "")
            {
                string[] CentralExciseDate = TxtDateCentralExcise.Text.Split(new char[] { '/' });
                centralexcise = CentralExciseDate[1].ToString() + "/" + CentralExciseDate[0].ToString() + "/" + CentralExciseDate[2].ToString();
            }

            if (DateLuxuryTax.Text.Trim() != "")
            {
                string[] datetaxluxury = DateLuxuryTax.Text.Split(new char[] { '/' });
                luxury = datetaxluxury[1].ToString() + "/" + datetaxluxury[0].ToString() + "/" + datetaxluxury[2].ToString();
            }



            int isvat = 0, iscst = 0, issst = 0, isce = 0, islt = 0;
            if (CHKVat.Checked == true)
            {
                isvat = 1;
            }
            if (CHKInterstate.Checked == true)
            {
                iscst = 1;
            }
            if (CHKServiceTax.Checked == true)
            {
                issst = 1;
            }
            if (CHKCentralExcise.Checked == true)
            {
                isce = 1;
            }
            if (CHKLuxuryTax.Checked == true)
            {
                islt = 1;
            }

            
            SqlParameter[] parm = {
                                   new SqlParameter("@CompId",Session["NewCompId"].ToString()),
                                    new SqlParameter("@Pan", TxtPermanant.Text.Trim()),
                                    new SqlParameter("@PanDate", Pan),
                                    new SqlParameter("@TDSNO", TxtTaxdeduction.Text.Trim()),
                                    new SqlParameter("@TDSDate", taxdeduct),
                                    new SqlParameter("@IsVatEnable",isvat),
                                    new SqlParameter("@VatNo",TxtBoxVat.Text.Trim()),
                                    new SqlParameter("@VatDate", vat  ),
                                    new SqlParameter("@IsCSTEnable",iscst),
                                    new SqlParameter("@CSTNo",TextBoxinnerState.Text.ToString()),
                                    new SqlParameter("@CSTDate",StateTax),
                                    new SqlParameter("@ISSTEnabled",issst),
                                    new SqlParameter("@SERTNo",TextBoxServiceTax.Text.Trim()),
                                    new SqlParameter("@SERTNDate",TaxService),
                                    new SqlParameter("@ISCEEnabled",isce),
                                    new SqlParameter("@CEPAN",TextBoxCentralExcise.Text.Trim()),
                                    new SqlParameter("@CEPANDate",centralexcise),
                                    new SqlParameter("@ISLTENABLED",islt),
                                    new SqlParameter("@LTNO", TextBoxLuxuryTax.Text.ToString()),
                                    new SqlParameter("@LTNDATE",luxury),
                                     
                                };
            try
            {
                dut.executeprocedure("usp_insert_ACC_Company2", parm);
                TabContainer1.ActiveTabIndex = 0;
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Company Registration details Created successfully...";
            }
            catch (Exception ex)
            {
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = ex.Message;
            }
        }
        
    }

    //protected void CHKVat_CheckChanged(object o, EventArgs e)
    //{
    //    if (CHKVat.Checked)
    //    {
    //        TxtBoxVat.Enabled = true;
    //        TxtVatDate.Enabled = true;
    //    }
    //    else
    //    {
    //        TxtBoxVat.Enabled = false;
    //        TxtVatDate.Enabled =false;
    //    }
    //}

    //protected void CHKInterstate_CheckChanged(object o, EventArgs e)
    //{
    //    if (CHKInterstate.Checked)
    //    {
    //        TextBoxinnerState.Enabled = true;
    //        TextBoxInterStateDate.Enabled = true;
    //    }
    //    else
    //    {
    //        TextBoxinnerState.Enabled = false;
    //        TextBoxInterStateDate.Enabled = false;
    //    }

    //}

    //protected void CHKServiceTax_CheckChanged(object o, EventArgs e)
    //{
    //    if (CHKServiceTax.Checked)
    //    {
    //        TextBoxServiceTax.Enabled = true;
    //        TxtDateServiceTax.Enabled = true;
    //    }
    //    else
    //    {
    //        TextBoxServiceTax.Enabled = false;
    //        TxtDateServiceTax.Enabled = false;
    //    }
    //}

    //protected void CHKCentralExcise_CheckChanged(object o, EventArgs e)
    //{
    //    if (CHKCentralExcise.Checked)
    //    {
    //        TextBoxCentralExcise.Enabled = true;
    //        TxtDateCentralExcise.Enabled = true;
    //    }
    //    else
    //    {
    //        TextBoxCentralExcise.Enabled = false;
    //        TxtDateCentralExcise.Enabled = false;
    //    }
    //}

    //protected void CHKLuxuryTax_CheckChanged(object o, EventArgs e)
    //{
    //    if (CHKLuxuryTax.Checked)
    //    {
    //        TextBoxLuxuryTax.Enabled = true;
    //        DateLuxuryTax.Enabled = true;
    //    }
    //    else
    //    {
    //        TextBoxLuxuryTax.Enabled = false;
    //        DateLuxuryTax.Enabled = false;
    //    }
    //}

    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("CompanyView.aspx");

    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("CompanyView.aspx");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("CompanyView.aspx");

    }
}


